package com.test.langchain4j.controller;

import com.test.langchain4j.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/8/4 11:16
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
@Slf4j
public class RAGController {
  private static final String DOCUMENT_PATH = "E:\\alibaba_java_v1.3.0.docx";
  @Resource InMemoryEmbeddingStore<TextSegment> embeddingStore;

  @Resource ChatAssistant chatAssistant;

  // http://localhost:9013/rag/add
  @GetMapping(value = "/rag/add")
  public String testAdd() throws FileNotFoundException {
    //    Document document = FileSystemDocumentLoader.loadDocument(DOCUMENT_PATH);

    FileInputStream fileInputStream = new FileInputStream(DOCUMENT_PATH);
    Document document = new ApacheTikaDocumentParser().parse(fileInputStream);
    EmbeddingStoreIngestor.ingest(document, embeddingStore);
    String result = chatAssistant.chat("如何进行单元测试");
    log.info(result);
    return result;
  }
}
